/**
 * AjLog - The AspectJ based logging facade
 * 
 * Copyright (C) 2009 - 2010, Marko Umek. All rights reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.ajlog.core.message;

import java.util.LinkedList;
import java.util.List;

import org.ajlog.core.message.segment.ConstantMessageSegment;
import org.ajlog.core.message.segment.FixedMessageSegement;
import org.ajlog.core.message.segment.IConstantMessageSegment;
import org.ajlog.core.message.segment.IMessageSegment;
import org.ajlog.core.message.segment.MessageSegmentFactory;
import org.ajlog.util.AjLogLogger;
import org.apache.commons.lang.StringUtils;

/**
 * MessageParserResultCollector is the standard implementation of
 * {@link IMessageParserResultCollector}.
 * 
 * @author Marko Umek
 * 
 */
public class MessageParserResultCollector
    implements IMessageParserResultCollector {

    private static final AjLogLogger    logger             = AjLogLogger.getLogger(MessageParserResultCollector.class);
    private final List<IMessageSegment> messageSegmentList = new LinkedList<IMessageSegment>();
    private MessageSegmentFactory       messageSegmentFactory;

    /**
     * Constructor
     * 
     */
    public MessageParserResultCollector() {
    }

    /**
     * Setter for messageSegmentFactory
     * 
     * @param messageSegmentFactory
     *            the messageSegmentFactory to set
     */
    public void setMessageSegmentFactory(final MessageSegmentFactory messageSegmentFactory) {
        this.messageSegmentFactory = messageSegmentFactory;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.ajlog.core.message.IMessageParserResultCollector#appendFixedMessage
     * (java.lang.String)
     */
    public void appendFixedMessage(final String fixedMessagePart) {
        logger.debug("Add constant message segment (value='%s')", fixedMessagePart);
        this.messageSegmentList.add(new ConstantMessageSegment(fixedMessagePart));
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.ajlog.core.message.IMessageParserResultCollector#appendVariable(java
     * .lang.String)
     */
    public boolean appendVariable(final String variable) {
        final IMessageSegment messageSegment = this.messageSegmentFactory.createMessageSegment(variable);
        if (messageSegment != null) {
            logger.debug("Created a potential dynamic message segment (value='%s')", variable);
            this.messageSegmentList.add(messageSegment);
            return true;
        }
        logger.warn("No message segemnt could be created (value='%s')", variable);

        return false;
    }

    /**
     * resolveMessageSegments is responsible for creating an (optimized -
     * reduced) message segment list. <br>
     * <br>
     * Note: The message segment list has no more
     * {@link IConstantMessageSegment} items.
     * 
     * @return optimized message segment list
     */
    public List<IMessageSegment> resolveMessageSegments() {
        final List<IMessageSegment> optimizedMessageSegmentList = new LinkedList<IMessageSegment>();
        StringBuilder messageBuilder = new StringBuilder("");

        for (final IMessageSegment segment : this.messageSegmentList) {
            if (segment instanceof IConstantMessageSegment) {
                ((IConstantMessageSegment) segment).buildConstantMessage(messageBuilder);
            }
            else {
                final String fixedMessagePart = messageBuilder.toString();
                if (StringUtils.isNotEmpty(fixedMessagePart)) {
                    optimizedMessageSegmentList.add(new FixedMessageSegement(fixedMessagePart));
                    messageBuilder = new StringBuilder("");
                }
                optimizedMessageSegmentList.add(segment);
            }
        }

        return optimizedMessageSegmentList;

    }
}
